package cn.yhbk.web.modules.manager.Service.Impl;

import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.modules.manager.Mapper.TbClassMapper;
import cn.yhbk.web.modules.manager.Mapper.TbCollegeMapper;
import cn.yhbk.web.modules.manager.Mapper.TbGradeMapper;
import cn.yhbk.web.modules.manager.Pojo.DTO.Class.ClassParam;
import cn.yhbk.web.modules.manager.Pojo.DTO.Grade.GradeAll;
import cn.yhbk.web.modules.manager.Pojo.DTO.Grade.GradeParam;
import cn.yhbk.web.modules.manager.Pojo.DTO.Major.MajorAll;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbCollege;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbMajor;
import cn.yhbk.web.modules.manager.Mapper.TbMajorMapper;
import cn.yhbk.web.modules.manager.Pojo.VO.Major.MajorPag;
import cn.yhbk.web.modules.manager.Pojo.DTO.Major.MajorParam;
import cn.yhbk.web.modules.manager.Service.ITbMajorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 专业表 服务实现类
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Service
public class TbMajorServiceImpl extends ServiceImpl<TbMajorMapper, TbMajor> implements ITbMajorService {

    @Autowired
    private TbMajorMapper tbMajorMapper;

    @Autowired
    private TbGradeMapper tbGradeMapper;

    @Autowired
    private TbClassMapper tbClassMapper;

    @Autowired
    private TbCollegeMapper tbCollegeMapper;

    @Override
    public MajorPag ManageGetMajorInfo(Integer currentPage, Integer pageSize, Long collegeId) {
        MajorPag majorPag = new MajorPag();
        majorPag.setPageSize(pageSize);
        majorPag.setCurrentPage(currentPage);
        majorPag.setTotalPage(tbMajorMapper.queryTotalNum(collegeId));
        majorPag.setMajorList(tbMajorMapper.queryMajorPage((currentPage - 1) * pageSize, pageSize, collegeId));
        return majorPag;
    }

    @Override
    public List<MajorAll> getMajorAll() {
        List<MajorAll> res = new ArrayList<>();
        // 查询所有专业
        List<MajorParam> majorParam = tbMajorMapper.getMajorAll();
        // 查询所有年级
        List<GradeParam> gradeParam = tbGradeMapper.getGradeIdAndNameAll();
        // 查询所有班级
        List<ClassParam> classParam = tbClassMapper.getClassAll();
        for(MajorParam majorParamItem: majorParam){
            MajorAll majorAll = new MajorAll();
            majorAll.setValue(majorParamItem.getId());
            majorAll.setLabel(majorParamItem.getMajorName());
            List<GradeAll> gradeAllList = new ArrayList<>();
            for(GradeParam gradeParamItem: gradeParam){
                GradeAll gradeAll = new GradeAll();
                gradeAll.setLabel(gradeParamItem.getNumber());
                gradeAll.setValue(gradeParamItem.getId());
                List<ClassParam> classAllList = new ArrayList<>();
                for(ClassParam classParamItem: classParam){
                    if(classParamItem.getGradeId().equals(gradeParamItem.getId())
                    && classParamItem.getMajorId().equals(majorParamItem.getId())){
                        classAllList.add(classParamItem);
                    }
                }
                gradeAll.setChildren(classAllList);
                gradeAllList.add(gradeAll);
            }
            majorAll.setChildren(gradeAllList);
            res.add(majorAll);
        }
        return res;
    }

    @Override
    public MajorPag getMajorInfoStuOffPag(Integer currentPage, Integer pageSize) {
        // 获取账号
        Long currentAdminId = MyUtil.getCurrentAdminId();
        // 获取学院ID
        QueryWrapper<TbCollege> tbCollegeQueryWrapper = new QueryWrapper<>();
        tbCollegeQueryWrapper.eq("stu_work_office_id", currentAdminId);
        TbCollege tbCollege = tbCollegeMapper.selectOne(tbCollegeQueryWrapper);
        // 获取数据
        MajorPag majorPag = new MajorPag();
        majorPag.setPageSize(pageSize);
        majorPag.setCurrentPage(currentPage);
        majorPag.setTotalPage(tbMajorMapper.queryTotalNum(tbCollege.getId()));
        majorPag.setMajorList(tbMajorMapper.queryMajorPage((currentPage - 1) * pageSize, pageSize, tbCollege.getId()));
        return majorPag;
    }
}